package com.hudun.permissionmanager;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.hudun.permissionmanager.rom.HuaweiUtils;
import com.hudun.permissionmanager.rom.MeizuUtils;
import com.hudun.permissionmanager.rom.MiuiUtils;
import com.hudun.permissionmanager.rom.OppoUtils;
import com.hudun.permissionmanager.rom.QikuUtils;
import com.hudun.permissionmanager.rom.RomUtils;

public class PermissionUtil {

    private static final String TAG = "PermissionsUtil";

    private PermissionFragment fragment;

    public PermissionUtil(@NonNull FragmentManager fragmentManager) {
        fragment = getPermissionsFragment(fragmentManager);
    }

    private PermissionFragment getPermissionsFragment(FragmentManager fragmentManager) {
        PermissionFragment fragment = (PermissionFragment) fragmentManager.findFragmentByTag(TAG);
        boolean isNewInstance = fragment == null;
        if (isNewInstance) {
            fragment = new PermissionFragment();
            fragmentManager
                    .beginTransaction()
                    .add(fragment, TAG)
                    .commit();
            fragmentManager.executePendingTransactions();
        }

        return fragment;
    }

    /**
     * 外部调用申请权限
     * @param permissions 申请的权限
     * @param listener 监听权限接口
     */
    public void requestMultiPermissions(int requestCode,String[] permissions, PermissionListener listener) {

        if (Build.VERSION.SDK_INT < 23){
            listener.onGranted(requestCode);
            return;
        }

        fragment.setListener(listener);
        fragment.requestMultiPermissions(requestCode,permissions);

    }

    public boolean CheckIsHavePermision(FragmentActivity activity,String Permission){
        int perm = activity.checkCallingOrSelfPermission(Permission);
        return perm == PackageManager.PERMISSION_GRANTED;
    }


    public boolean CheckIsHavePermision(FragmentActivity activity,String[] Permissions){
        for (int i = 0 ; i < Permissions.length ; i++){
            int perm = activity.checkCallingOrSelfPermission(Permissions[i]);
            if (perm == PackageManager.PERMISSION_DENIED){
                return false;
            }
        }
        return true;
    }

    /**
     * 外部调用申请权限
     * @param permission 申请的权限
     * @param listener 监听权限接口
     */
    public void requestOnePermission(int requestCode,String permission, PermissionListener listener) {
        if (Build.VERSION.SDK_INT < 23){
            listener.onGranted(requestCode);
            return;
        }

        fragment.setListener(listener);
        fragment.requestOnePermission(requestCode,permission);

    }

    /**
     * 打开应用的系统权限设置页面,用的系统的隐式intent打开set界面
     *
     * @param context
     */
    public static void toPermissionSettingSystemWay(Context context) {
        if (context!=null){
            final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            }
        }

    }
    /**
     * 跳转到权限设置界面，网上的代码，说是适配了各大手机厂商的ROM，但是发现个别手机提示App会悬浮到其他App上。
     *
     * @param context
     */
    public static void toPermissionSettingAdaptROMWay(Context context) throws NoSuchFieldException, IllegalAccessException {
        if (Build.VERSION.SDK_INT < 23) {
            if (RomUtils.checkIsMiuiRom()) {
                MiuiUtils.applyMiuiPermission(context);
            } else if (RomUtils.checkIsMeizuRom()) {
                MeizuUtils.applyPermission(context);
            } else if (RomUtils.checkIsHuaweiRom()) {
                HuaweiUtils.applyPermission(context);
            } else if (RomUtils.checkIs360Rom()) {
                QikuUtils.applyPermission(context);
            } else if (RomUtils.checkIsOppoRom()) {
                OppoUtils.applyOppoPermission(context);
            } else {
                RomUtils.getAppDetailSettingIntent(context);
            }
        } else {
            if (RomUtils.checkIsMeizuRom()) {
                MeizuUtils.applyPermission(context);
            } else {
                if (RomUtils.checkIsOppoRom() || RomUtils.checkIsVivoRom()
                        || RomUtils.checkIsHuaweiRom() || RomUtils.checkIsSamsunRom()) {
                    RomUtils.getAppDetailSettingIntent(context);
                } else if (RomUtils.checkIsMiuiRom()) {
                    MiuiUtils.toPermisstionSetting(context);
                } else {
                    RomUtils.commonROMPermissionApplyInternal(context);
                }
            }
        }
    }

}